﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class MakeReservation : Form
    {
        public MakeReservation()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime stDate = startDate.SelectionStart;
            DateTime endDate = startDate.SelectionEnd;
            int NoOfGuest = Convert.ToInt32(txtNoOfGuest.Text);
          
            List<RoomTypeService.RoomType> roomTypes = rtClient.getAvailableRoomType(stDate, endDate, NoOfGuest).ToList();
            grid.DataSource = roomTypes;
            string freeRoomType = "";
            foreach (RoomTypeService.RoomType rt in roomTypes)
            {
                freeRoomType +="ID " + rt.id + ", Room Type "+rt.RoomTypeName + ", " + "No Of Bed" + rt.NoOfBed + "\n";
            }

            lblAvailableRoom.Text = freeRoomType;

        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            DateTime stDate = startDate.SelectionStart;
            DateTime endDate = startDate.SelectionEnd;
           
            if (!chkAsync.Checked)
            {

                BookingService.Booking b = new BookingService.Booking();
                b.startDate = stDate;
                b.endDate = endDate;
                b.roomTypeID = Convert.ToInt32(txtRoomType.Text);

                BookingService.Guest g = new BookingService.Guest();
                g.address = txtAddress.Text;
                g.city = txtCity.Text;
                g.country = txtCountry.Text;
                int month = Convert.ToInt32(txtExpMonth.Text);
                int year = Convert.ToInt32(txtExpYear.Text);

                DateTime expDate = new DateTime(year, month, 1);


                g.creditExpiryDate = expDate;
                g.creditNo = txtCreditCard.Text;
                g.cvv = txtCvv.Text;
                g.email = txtEmail.Text;
                g.firstName = txtFastName.Text;
                g.lastName = txtLastName.Text;
                g.creditExpiryDate = expDate;
                g.postal = txtPostal.Text;
                g.phone = txtPhone.Text;

                List<BookingService.Guest> guestList = new List<BookingService.Guest>();
                guestList.Add(g);
                String result = bClient.MakeReservation(b, guestList.ToArray());

                if (!String.IsNullOrEmpty(result))
                {
                    MessageBox.Show(result);
                    lblBookingNumber.Text = result;
                }
                else
                {
                    MessageBox.Show("Booking Failed");
                }
            }
            else
            {

                AsyncBooking.Booking b = new AsyncBooking.Booking();
                b.startDate = stDate;
                b.endDate = endDate;
                b.roomTypeID = Convert.ToInt32(txtRoomType.Text);

                AsyncBooking.Guest g = new AsyncBooking.Guest();
                g.address = txtAddress.Text;
                g.city = txtCity.Text;
                g.country = txtCountry.Text;
                int month = Convert.ToInt32(txtExpMonth.Text);
                int year = Convert.ToInt32(txtExpYear.Text);

                DateTime expDate = new DateTime(year, month, 1);


                g.creditExpiryDate = expDate;
                g.creditNo = txtCreditCard.Text;
                g.cvv = txtCvv.Text;
                g.email = txtEmail.Text;
                g.firstName = txtFastName.Text;
                g.lastName = txtLastName.Text;
                g.creditExpiryDate = expDate;
                g.postal = txtPostal.Text;
                g.phone = txtPhone.Text;

                List<AsyncBooking.Guest> guestList = new List<AsyncBooking.Guest>();
                guestList.Add(g);
                AsyncBooking.BookingServiceClient ac = new AsyncBooking.BookingServiceClient();
                AsyncCallback cb = new AsyncCallback(HelloServiceCallBack);
                ac.BeginMakeReservation(b, guestList.ToArray(), cb,ac);
                
            }

        }


        private   void HelloServiceCallBack(IAsyncResult ar)
        {
           
            AsyncBooking.BookingServiceClient ac = (AsyncBooking.BookingServiceClient)ar.AsyncState;


            String result = ac.EndMakeReservation(ar);

            if (!String.IsNullOrEmpty(result))
            {
                MessageBox.Show(result); 
            }
            else
            {
                MessageBox.Show("Booking Failed");
            }
            

        }
        BookingService.BookingServiceClient bClient;
        RoomTypeService.RoomTypeServiceClient rtClient;
        private void MakeReservation_Load(object sender, EventArgs e)
        {
            bClient = new BookingService.BookingServiceClient();
            bClient.ClientCredentials.Windows.ClientCredential.UserName = "Nyan";
            bClient.ClientCredentials.Windows.ClientCredential.Password = "P@55w0rd";
            rtClient = new RoomTypeService.RoomTypeServiceClient();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GuestPage gp = new GuestPage();
            gp.Show();
        }

      
    

 
 
 

  

       
    }
}
